package com.py.korona.providers.base;

import android.content.ContentValues;
import android.content.UriMatcher;
import android.net.Uri;

public class SimpleDBContentProvider extends DBContentProvider {
    public enum SQLOperand {
        SQLOperandAND(" AND "), SQLOperandOR(" OR ");
        private final String operand;

        private SQLOperand(final String text) {
            this.operand = text;
        }

        @Override
        public String toString() {
            return operand;
        }
    }
    protected static final int URICodeMatchFilter = 20;
    protected static final int URICodeMatchItems  = 10;
    protected static final int URICodeMatchList   = 30;

    @Override
    public Uri insertUri(long id) {
        Uri result = Uri.parse(getUriBasePath() + "?" + fieldNameID() + "=" + id);
        return result;
    }

    @Override
    protected void addAvailableURI(UriMatcher uriMatcher) {
        uriMatcher.addURI(getUriAuthority(), getUriBasePath(), URICodeMatchItems);
        uriMatcher.addURI(getUriAuthority(), getUriBasePath() + "/filter", URICodeMatchFilter);
        uriMatcher.addURI(getUriAuthority(), getUriBasePath() + "/filter/*", URICodeMatchFilter);
        uriMatcher.addURI(getUriAuthority(), getUriBasePath() + "/list", URICodeMatchList);
        uriMatcher.addURI(getUriAuthority(), getUriBasePath() + "/list/*", URICodeMatchList);
    }

    @Override
    protected void allColumnNames() {
    }

    @Override
    protected String fieldNameID() {
        return "_id";
    }

    @Override
    protected String getSQLDeleteWhereClause(UriMatcher uriMatcher, Uri uri, String selection, String[] selectionArgs) {
        String result = null;
        int uriCode = uriMatcher.match(uri);
        switch (uriCode) {
            case URICodeMatchFilter: {
                result = uriQueryToSQL(uri, SQLOperand.SQLOperandAND);
                break;
            }
            case URICodeMatchItems: {
                result = null;
                break;
            }
            default: {
            }
        }
        return result;
    }

    @Override
    protected String getSQLGroupbyFields(UriMatcher uriMatcher, Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected String getSQLSelectWhereClause(UriMatcher uriMatcher, Uri uri, String selection, String[] selectionArgs) {
        String result = null;
        int uriCode = uriMatcher.match(uri);
        switch (uriCode) {
            case URICodeMatchItems: {
                //FIXME: create valid result using selectionArgs
                result = null;
                break;
            }
            case URICodeMatchFilter: {
                result = uriQueryToSQL(uri, SQLOperand.SQLOperandAND);
                break;
            }
            case URICodeMatchList: {
                result = uriQueryToSQL(uri, SQLOperand.SQLOperandOR);
                break;
            }
            default: {
            }
        }
        return result;
    }

    @Override
    protected String getSQLTableName(UriMatcher uriMatcher, Uri uri) {
        return null;
    }

    @Override
    protected String getSQLUpdateWhereClause(UriMatcher uriMatcher, Uri uri, String selection, String[] selectionArgs) {
        String result = null;
        int uriCode = uriMatcher.match(uri);
        switch (uriCode) {
            case URICodeMatchItems: {
                result = selection;
                break;
            }
            case URICodeMatchFilter: {
                result = uriQueryToSQL(uri, SQLOperand.SQLOperandAND);
                break;
            }
            default: {
            }
        }
        return result;
    }

    @Override
    protected String getUriAuthority() {
        return null;
    }

    @Override
    protected String getUriBasePath() {
        return null;
    }

    @Override
    protected void initialize() {
    }

    @Override
    protected boolean insertAllowed(UriMatcher uriMatcher, Uri uri, ContentValues values) {
        boolean result = false;
        int uriCode = uriMatcher.match(uri);
        switch (uriCode) {
            case URICodeMatchItems: {
                result = true;
                break;
            }
        }
        return result;
    }

    protected String uriQueryToSQL(Uri uri, SQLOperand operand) {
        boolean hasAnd = false;
        StringBuilder builder = new StringBuilder();
        for (String paramName : uri.getQueryParameterNames()) {
            String paramValue = uri.getQueryParameter(paramName);
            String[] values = paramValue.split("\\,");
            for (String value : values) {
                builder.append((hasAnd ? operand.toString() : " ") + paramName + "=" + value);
                hasAnd = true;
            }
        }
        return builder.toString();
    }
}
